package demo28;

/**
 * 改进：不锁住整个方法，只锁住其中的new语句就OK。就是所谓的“双重锁”机制
 */
public class Singleton3 {
    private static Singleton3 instance;
    private Singleton3(){
    }
    public static Singleton3 getInstance(){    //对获取实例的方法进行同步
        if (instance == null){
            synchronized(Singleton3.class){
                if (instance == null)
                    instance = new Singleton3();
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        Runnable runnable = () -> System.out.println(Singleton3.getInstance());
        new Thread(runnable,"t1").start();
        new Thread(runnable,"t2").start();
    }

}